package com.ruoyi.web.controller.system;
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

public class WordWatermark {
    public static void main(String[] args) {
        //加载示例文档
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\应急广播new\\红谷滩应急广播项目验收资料-承建单位(施工单位)需用印20240104修改打印版.docx");
        //添加艺术字并设置大小
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.setWidth(430);
        shape.setHeight(10);
        //设置艺术字文本内容、位置及样式
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(325);
        shape.getWordArt().setFontFamily("宋体");
        shape.getWordArt().setText("2023年红谷滩区应急广播系统建设项目验收资料");
        shape.setFillColor(new Color(192, 192, 192, 255));
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(new Color(192, 192, 192, 255));
        shape.setStrokeWeight(1);

        Section section;
        HeaderFooter header;
        for (int n = 0; n < doc.getSections().getCount(); n++) {
            section = doc.getSections().get(n);
            //获取section的页眉
            header = section.getHeadersFooters().getHeader();
            Paragraph paragraph;

            if (header.getParagraphs().getCount() > 0) {
                //如果页眉有段落，取它第一个段落
                paragraph = header.getParagraphs().get(0);
            } else {
                //否则新增加一个段落到页眉
                paragraph = header.addParagraph();
            }
            for (int i = 0; i < 1; i++) {
                for (int j = 0; j < 1; j++) {
                    //复制艺术字并设置多行多列位置
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(140 + 150 * i);
                    shape.setHorizontalPosition(-110+350*j);
                    paragraph.getChildObjects().add(shape);
                }
            }
            for (int i = 1; i < 2; i++) {
                for (int j = 0; j <1 ; j++) {
                    //复制艺术字并设置多行多列位置
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(255 + 150 * i);
                    shape.setHorizontalPosition(-185+350*j);
                    paragraph.getChildObjects().add(shape);
                }
            }
            for (int i = 2; i < 3; i++) {
                for (int j = 0; j < 1; j++) {
                    //复制艺术字并设置多行多列位置
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(50 + 150 * i);
                    shape.setHorizontalPosition(50+350*j);
                    paragraph.getChildObjects().add(shape);
                }
            }
            for (int i = 0; i < 1; i++) {
                for (int j = 1; j < 2; j++) {
                    //复制艺术字并设置多行多列位置
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(140 + 150 * i);
                    shape.setHorizontalPosition(-260+300*j);
                    paragraph.getChildObjects().add(shape);
                }
            }
         /*   for (int i = 1; i < 2; i++) {
                for (int j = 1; j <2 ; j++) {
                    //复制艺术字并设置多行多列位置
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(145 + 150 * i);
                    shape.setHorizontalPosition(-180+300*j);
                    paragraph.getChildObjects().add(shape);
                }
            }*/
            for (int i = 2; i < 3; i++) {
                for (int j = 1; j < 2; j++) {
                    //复制艺术字并设置多行多列位置
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(205 + 150 * i);
                    shape.setHorizontalPosition(-310+300*j);
                    paragraph.getChildObjects().add(shape);
                }
            }
        }
        //保存文档
        doc.saveToFile("C:\\Users\\Administrator\\Desktop\\应急广播new\\result.docx", FileFormat.Docx_2013);
    }
}
